// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AStar from "./Astar";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Game extends cc.Component {

    @property(cc.Node)
    canWalkItem: cc.Node = null;

    @property(cc.Node)
    notWalkItem: cc.Node = null;

    // @property([cc.Node])
    player: cc.Node[] = [];

    mapRows:number
    mapCols:number
    itemWH = 100;
    _path:cc.Vec2[]
    // LIFE-CYCLE CALLBACKS:
    _moveTarget:cc.Vec2;
    _speed:cc.Vec2;

    onLoad () {
        this.itemWH = this.canWalkItem.width;
        this.mapCols = Math.floor(cc.winSize.width / this.itemWH);
        this.mapRows = Math.floor(cc.winSize.height / this.itemWH);
        this.player.push(cc.find("player",this.node))
        this.player.push(cc.find("player1",this.node))
        this.player.push(cc.find("player2",this.node))
        this.player.push(cc.find("player3",this.node))
        this.player.push(cc.find("player4",this.node))
        this.player.push(cc.find("player5",this.node))
        this.player.push(cc.find("player6",this.node))
        window["Game"] = this
    }

    start () {
        let leftBottomX = -cc.winSize.width *0.5+ this.itemWH/2;
        let leftBottomY = -cc.winSize.height *0.5+ this.itemWH/2;
        AStar.instance.init(this.mapRows,this.mapCols,leftBottomX,leftBottomY,this.itemWH,this.itemWH);
        let canWalkPos:cc.Vec2[] = []
        for(var i = 0; i < this.mapCols; i++){
            for(var j = 0; j < this.mapRows; j++){
                let random = Math.random();
                let canWalk = random > 0.2
                let node = canWalk ? this.canWalkItem : this.notWalkItem;
                let mapItem = cc.instantiate(node);
                mapItem.x = leftBottomX + i * this.itemWH;
                mapItem.y = leftBottomY + j * this.itemWH;
                mapItem.parent = cc.find("Map",this.node)
                AStar.instance.setWalkable(i,j,canWalk)
                if(canWalk){
                    canWalkPos.push(mapItem.getPosition())
                }
            }
        }
        for (let i = 0; i < this.player.length; i++) {
            const element = this.player[i];
            element.addComponent("Player");
            element.setPosition(canWalkPos.splice(Math.floor(Math.random()*canWalkPos.length),1)[0])
        }
        // this.player.x = leftBottomX;
        // this.player.y = leftBottomY;

        cc.find("Canvas").on(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this)
    }

    onTouchEnd(e:cc.Event.EventTouch){
        // let startX = this.player.x + this.player.parent.x;
        // let startY = this.player.y + this.player.parent.y;
        // startX = Math.floor(startX / this.itemWH)
        // startY = Math.floor(startY / this.itemWH)
        // let endPos = e.getLocation();
        // let endX = Math.floor(endPos.x / this.itemWH)
        // let endY = Math.floor(endPos.y / this.itemWH)
        // console.time("1")
        // let find = AStar.instance.findPath(startX,startY,endX,endY);
        // console.timeEnd("1")
        // if(find){
        //     this._path = AStar.instance.path;
        //     this.nextTarget();
        // }
        // console.time("1")
        for (let i = 0; i < this.player.length; i++) {
            const element = this.player[i];
            element.getComponent("Player").onTouchEnd(e);
        }
        // console.timeEnd("1")
    }

    // nextTarget(){
    //     this._moveTarget = null;
    //     this._speed = null;
    //     if(this._path && this._path.length > 0){
    //         this._moveTarget = this._path.shift();
    //         let nowPos = this.player.getPosition();
    //         let dir = this._moveTarget.sub(nowPos);
    //         this._speed = dir.normalize().mul(500);
    //     }
    // }

    // update (dt) {
    //     if(this._speed){
    //         let dis = this._speed.mul(dt);
    //         let disLen = dis.len();
    //         let nowPos = this.player.getPosition();
    //         let sub = this._moveTarget.sub(nowPos);
    //         let subLen = sub.len();
    //         if(disLen >= subLen){
    //             disLen = subLen;
    //             this.player.setPosition(this._moveTarget);
    //             this.nextTarget();
    //             return;
    //         }else{
    //             this.player.setPosition(nowPos.add(dis));
    //         }
    //     }
    // }
}
